package kr.hkit.powerjava.chther23;

public class ThreadControl {
	static void print(String msg){
		String threadName = Thread.currentThread().getName();
		System.out.format("%s : %s%n",threadName,msg);
		
	}
	private static class MessageLoop implements Runnable{

		@Override
		public void run() {
			String[] message = {"pride will have afall.","power is dangerous unless you have humility","office changes manners.","empty vessels make the most sound."};
			for(int i=0;i<message.length;i++){
				print(message[i]);
				try {Thread.sleep(2000);
				} catch (InterruptedException e) {
					print("아직 끝나지 않았음");
				}
			}
		}
	
	}
	public static void main(String[] args) throws InterruptedException {
		int tries =0;//시도횟수
		ThreadControl.print("추가적인 스레드를 시작합니다.");
		Thread t= new Thread(new MessageLoop());
		t.start();
		
		ThreadControl.print("추가적인 스레드를 끝나기를 기다립니다.");
		
		while(t.isAlive()){
//			print("아직 기다립니다.");
			ThreadControl.print("아직 기다립니다.");
//			System.out.println("is Alive call");
			t.join(1000);
			tries++;
			System.out.println(tries);
			if(tries>2){
				ThreadControl.print("참을수없네요~~");
				t.interrupt();
				t.join();
			}
		}
		ThreadControl.print("메인스레드 종료");
		
	}
}
